<?php
add_action('admin_init', 'growth_calculator_options_init' );
add_action('admin_menu', 'growth_calculator_options_add_page');

function growth_calculator_options_init(){
    register_setting( 'growth_calculator_options', 'growth_calculator_opt', 'growth_calculator_options_validate' );
}

// Add menu page
function growth_calculator_options_add_page() {
	add_options_page('Footfall Growth Calculator Options', 'Footfall Growth Calculator Options', 'manage_options', 'ff-growthcalculator', 'growth_calculator_options_do_page');
}

// Draw the menu page itself
function growth_calculator_options_do_page() {
?>
	<div class="wrap">
		<h2>Growth Calculator Options</h2>
		<form method="post" action="options.php">
			<?php settings_fields('growth_calculator_options'); ?>
			<?php $options = get_option('growth_calculator_opt'); ?>
			<table class="form-table">
				<tr valign="top"><th scope="row">Calculator Position</th>
					<td><select name="growth_calculator_opt[position]" size="1">
						<option value="popup" <?php if($options['position'] == 'popup'){echo "selected=\"selected\"";} ?>><?php _e('Pop-up','ff-growth-calculator'); ?></option>
						<option value="embed" <?php if($options['position'] == 'embed'){echo "selected=\"selected\"";} ?>><?php _e('Embed','ff-growth-calculator'); ?></option>
					</select>
					</td>
				</tr>
				<tr valign="top"><th scope="row">Get In Touch Link</th>
					<td><input type="text" name="growth_calculator_opt[theLink]" value="<?php echo $options['theLink']; ?>" size="72" /></td>
				</tr>
				<tr valign="top"><th scope="row">Contact Email Address</th>
					<td><input type="text" name="growth_calculator_opt[emailAddress]" value="<?php echo $options['emailAddress']; ?>" size="72" /></td>
				</tr>
				<tr valign="top"><th scope="row">Phone Number</th>
					<td><input type="text" name="growth_calculator_opt[thePhone]" value="<?php echo $options['thePhone']; ?>" size="72" /></td>
				</tr>
				<tr valign="top"><th scope="row">Open in new window</th>
					<td><input name="growth_calculator_opt[newWindow]" type="checkbox" value="1" <?php checked('1', $options['newWindow']); ?> /></td>
				</tr>
			</table>
			<p class="submit">
			<input type="submit" class="button-primary" value="<?php _e('Save Changes','ff-growth-calculator') ?>" />
			</p>
		</form>
	</div>
<?php
}

// Sanitize and validate input. Accepts an array, return a sanitized array.
function growth_calculator_options_validate($input) {
	// Our first value is either 0 or 1
	$input['newWindow'] = ( $input['newWindow'] == 1 ? 1 : 0 );
	
	// Say our second option must be safe text with no HTML tags
	$input['theLink'] =  wp_filter_nohtml_kses($input['theLink']);
	$input['thePhone'] =  wp_filter_nohtml_kses($input['thePhone']);
	$input['emailAddress'] =  wp_filter_nohtml_kses($input['emailAddress']);
	
	return $input;
}
?>